import { defineStore } from 'pinia'
import { ref } from 'vue'
import { BindAndReturnAddressType } from '@/types/interface'
import { PointType } from '@/types/enum'
export const useMapPoint = defineStore(
  'useMapPoint',
  () => {
    type TextType = '查看还车点' | '查看取车点'
    const type = ref<PointType>('return')
    const text = ref<TextType>('查看取车点')
    const returnAddress = ref<BindAndReturnAddressType[] | BindAndReturnAddressType>([])
    const bindAddress = ref<BindAndReturnAddressType[] | BindAndReturnAddressType>([])
    const changeType = () => {
      switch (type.value) {
        case 'take':
          setType('return')
          setText('查看取车点')
          break
        case 'return':
          setType('take')
          setText('查看还车点')
          break
        default:
          break
      }
    }
    const setReturnAddress = (data: BindAndReturnAddressType[] | BindAndReturnAddressType) => {
      returnAddress.value = data
      returnisArray.value = isArray(returnAddress.value)
    }
    const setBindAddress = (data: BindAndReturnAddressType[] | BindAndReturnAddressType) => {
      bindAddress.value = data
      takeisArray.value = isArray(bindAddress.value)
    }
    const getReturnAddress: () => BindAndReturnAddressType[] | BindAndReturnAddressType = () => {
      return returnAddress.value
    }
    const getTakeAddress: () => BindAndReturnAddressType[] | BindAndReturnAddressType = () => {
      return bindAddress.value
    }
    const isArray = (data: BindAndReturnAddressType[] | BindAndReturnAddressType) => {
      return Array.isArray(data)
    }
    const setType = (data: PointType) => {
      type.value = data
    }
    const getType = () => {
      return type.value
    }
    const setText = (data: TextType) => {
      text.value = data
    }
    const getText = () => {
      return text.value
    }
    const getReturnIsArray = () => {
      return returnisArray.value
    }
    const getTakeIsArray = () => {
      return takeisArray.value
    }
    const returnisArray = ref<boolean>(isArray(returnAddress.value))
    const takeisArray = ref<boolean>(isArray(bindAddress.value))
    return {
      type,
      text,
      returnAddress,
      bindAddress,
      returnisArray,
      takeisArray,
      getText,
      getType,
      setType,
      setText,
      changeType,
      setReturnAddress,
      setBindAddress,
      getReturnAddress,
      getTakeAddress,
      getReturnIsArray,
      getTakeIsArray
    }
  },
  {
    persist: true
  }
)
